from threading import Thread
from socket import *

class CientT(Thread):
    def __init__(self,sock):
        Thread.__init__(self)
        self.sock = sock
    def run(self):
            while True:
                data = self.sock.recvfrom(1024)
                print("\r>>%s:%s\n<<"%(str(data[1]),data[0].decode("gb2312")))
                print("<<",end='')

class ServerT(Thread):
    def __init__(self,sock):
        Thread.__init__(self)
        self.sock = sock
    def run(self):
        sendIP = input("请输入对方的ip：")
        sendPort = input("请输入对方的端口：")
        while True:
            sendData = input("<<")
            self.sock.sendto(sendData.encode('gb2312'), (sendIP, int(sendPort)))

def main():
    socketUDP = socket(AF_INET, SOCK_DGRAM)
    socketUDP.bind(('', 7788))

    ct = CientT(socketUDP)
    st = ServerT(socketUDP)
    ct.start()
    st.start()

if __name__ == '__main__':
    main()



